<?php
/*                            _ooOoo_
 *                           o8888888o
 *                           88" . "88
 *                           (| -_- |)
 *                            O\ = /O
 *                        ____/`---'\____
 *                      .   ' \\| |// `.
 *                       / \\||| : |||// \
 *                     / _||||| -:- |||||- \
 *                       | | \\\ - /// | |
 *                     | \_| ''\---/'' | |
 *                      \ .-\__ `-` ___/-. /
 *                   ___`. .' /--.--\ `. . __
 *                ."" '< `.___\_<|>_/___.' >'"".
 *               | | : `- \`.;`\ _ /`;.`/ - ` : | |
 *                 \ \ `-. \_ __\ /__ _/ .-` / /
 *         ======`-.____`-.___\_____/___.-`____.-'======
 *                            `=---='
 *
 *         .............................................
 *                  佛祖保佑             永无BUG
 *
 * ======================================================
 * @author: Ethan Lu <ethan.lu@qq.com>
 * 
 */

namespace common\widgets;


use yii\bootstrap\ActiveForm;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\widgets\InputWidget;

class FormAttributeMultiple extends InputWidget
{
    public $attributeItems;

    private $formType = [
        'select' => 'activeDropDownList',
        'checkbox' => 'activeCheckboxList',
        'radio' => 'activeRadioList',
        'input' => 'activeTextInput',
    ];

    public function init()
    {
        parent::init();

        if($this->hasModel()) {
            $this->name = $this->name ?: Html::getInputName($this->model, $this->attribute);
            $this->value = $this->value ?: Html::getAttributeValue($this->model, $this->attribute);
        }
    }


    public function run()
    {
        $content = '';
        foreach($this->attributeItems as $k => $v) {
            $content .= $this->generationForm($v, $k);
        }
        return $content;
    }

    /**
     * 生成表单
     * @param $attributeData
     * @param $index
     * @return $this
     */
    protected function generationForm($attributeData, $index)
    {
        list($type, $attributeValue) = $this->getAttributeValue($attributeData);

        $content = Html::beginTag('div', ['class' => 'form-group']);
        $content .= Html::hiddenInput($this->name.'['.$attributeData->id.'][attribute_id]', $attributeData->id);
        $content .= Html::label($attributeData->name, null, ['class' => 'control-label col-sm-2']);
        if(in_array($type, ['activeDropDownList', 'activeCheckboxList', 'activeRadioList'])) {
            $css_class = [];
            if($type == 'activeCheckboxList') {
                $css_class = ['itemOptions' => ['labelOptions' => ['class' => 'checkbox-inline']]];
            };
            $content .= Html::$type($this->model, $this->attribute.'['.$attributeData->id.'][value]', $attributeValue, array_merge(['class' => 'col-sm-10'], $css_class));
        } elseif ($type == 'textInput') {
            $content .= Html::$type($this->model, $this->attribute.'['.$attributeData->id.'][value]', ['class' => 'col-sm-10']);
        }

        $content .= Html::endTag('div');
        return $content;
    }

    /**
     * 获取属性默认值
     * @param $attributeData
     * @return array
     */
    protected function getAttributeValue($attributeData)
    {
        if(in_array($attributeData->type, ['select', 'checkbox', 'radio'])) {
            return [
                $this->formType[$attributeData->type],
                ArrayHelper::map($attributeData->value, function($v) { return $v; }, function($v) { return $v; }),
            ];
        }
    }
}